/*
 * InputTextDialog.java
 * 
 * Copyright 2008 David D. Emory
 * 
 * This file is part of Five Points. See <http://www.fpdev.org> for
 * additional information regarding the project.
 * 
 * Five Points is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * 
 * Five Points is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with Five Points.  If not, see <http://www.gnu.org/licenses/>.
 */
package org.fpdev.apps.rtemaster.gui.dialogs;

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.border.*;
import org.fpdev.apps.rtemaster.RouteMaster;

public class InputTextDialog extends JDialog implements ActionListener {

  private JTextArea inputBox_;
  private JButton okBtn_,  clearBtn_,  cancelBtn_;
  private boolean okPressed_;

  public InputTextDialog(RouteMaster cv, String prompt, String initialText) {
    super(cv.getGUI(), "Input Text", true);

    okPressed_ = false;

    JLabel label = new JLabel(prompt);
    // ** input box ** //
    inputBox_ = new JTextArea(10, 25);
    inputBox_.setLineWrap(false);
    inputBox_.setText(initialText);
    JScrollPane inputPane = new JScrollPane(inputBox_);
    inputPane.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
    inputPane.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
    inputPane.setBorder(BorderFactory.createLoweredBevelBorder());

    okBtn_ = new JButton("OK");
    okBtn_.addActionListener(this);
    clearBtn_ = new JButton("Clear Text");
    clearBtn_.addActionListener(this);
    cancelBtn_ = new JButton("Cancel");
    cancelBtn_.addActionListener(this);

    JPanel btnRow = new JPanel();
    btnRow.setLayout(new BoxLayout(btnRow, BoxLayout.X_AXIS));
    btnRow.add(Box.createHorizontalGlue());
    btnRow.add(okBtn_);
    btnRow.add(Box.createHorizontalStrut(5));
    btnRow.add(clearBtn_);
    btnRow.add(Box.createHorizontalStrut(5));
    btnRow.add(cancelBtn_);
    btnRow.add(Box.createHorizontalGlue());


    JPanel mainPanel = new JPanel();
    mainPanel.setLayout(new BoxLayout(mainPanel, BoxLayout.Y_AXIS));

    label.setAlignmentX(LEFT_ALIGNMENT);
    mainPanel.add(label);
    mainPanel.add(Box.createVerticalStrut(5));
    inputPane.setAlignmentX(LEFT_ALIGNMENT);
    mainPanel.add(inputPane);
    mainPanel.add(Box.createVerticalStrut(6));
    btnRow.setAlignmentX(LEFT_ALIGNMENT);
    mainPanel.add(btnRow);

    mainPanel.setBorder(new EmptyBorder(5, 5, 5, 5));
    getContentPane().add(mainPanel);

    this.pack();
    this.setLocation((cv.getGUI().getWidth() / 2) - getWidth() / 2 + cv.getGUI().getX(),
            (cv.getGUI().getHeight() / 2) - getHeight() / 2 + cv.getGUI().getY());
    this.setVisible(true);

  }

  public void actionPerformed(ActionEvent e) {
    if (e.getSource() == okBtn_) {
      okPressed_ = true;
      this.setVisible(false);
    } else if (e.getSource() == clearBtn_) {
      inputBox_.setText("");
    } else if (e.getSource() == cancelBtn_) {
      this.setVisible(false);
    }
  }

  public boolean okPressed() {
    return okPressed_;
  }

  public String getText() {
    return inputBox_.getText();
  }
}
























